package com.pets.web.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.pets.common.utils.CDNUtil;
import com.pets.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class CDNUrlsJsonSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String urls, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (StringUtils.isEmpty(urls)) {
            return;
        }
        String[] urlArr = urls.split(",");
        List<String> signUrls = CDNUtil.signs(Arrays.asList(urlArr));
        jsonGenerator.writeString(String.join(",", signUrls));
    }

}
